package com.txh.blog.controller;

import com.github.pagehelper.PageInfo;
import com.txh.blog.bean.FirstPageBlog;
import com.txh.blog.bean.Type;
import com.txh.blog.service.BlogService;
import com.txh.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class TypesController{
    @Autowired
    private TypeService typeService;
    @Autowired
    private BlogService blogService;
    @RequestMapping("/types/{id}")
    public String types(@PathVariable Long id, @RequestParam(name = "page",required = true,defaultValue = "1") int page,
                        @RequestParam(name = "pageSize",required = true,defaultValue = "100")int pageSize, Model model){
        List<Type> types = typeService.listTypeSize();
        //-1表示从首页导航点进来的
        if (id == -1) {
            id = types.get(0).getId();
        }
        model.addAttribute("types", types);
        List<FirstPageBlog> blogs = blogService.getByTypeId(id,page,pageSize);
        PageInfo<FirstPageBlog> pageInfo = new PageInfo<>(blogs);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("activeTypeId", id);
        return "types";
    }
}
